package com.glamour.hlcity.module.spider.util;

import java.util.ArrayList;
import java.util.List;

import org.htmlparser.Tag;
import org.htmlparser.util.NodeList;

import com.glamour.hlcity.common.data.model.TagNode;
import com.glamour.hlcity.common.data.model.TextNode;
import com.glamour.hlcity.common.model.Node;

/**
 * html页面元素
 * @author zhaoxing.liuzx
 *
 */
public class HtmlElementUtil {
	
	/**
	 * 顺序1
	 */
	private final static Integer SIBER_1=1;
	
	/**
	 * 层级1
	 */
	private final static Integer LEVEL_1=1;
	
	/**
	 * 将一个树形结构节点转换成List
	 * @param node 树形节点
	 * @return
	 */
	public static List<Node> formList(Node node){
		List<Node> nodeList=new ArrayList<Node>();
		treeNodeToList(node, nodeList);
		return nodeList;
	}
	
	private static void treeNodeToList(Node node,List<Node> nodeList){
		if(node==null){
			return;
		}
		nodeList.add(node);
		if(node.getChildNodes()==null){
			return;
		}
		for(Node temp:node.getChildNodes()){
			treeNodeToList(temp, nodeList);
		}
	}
	

	/**
	 * 构建特定的树形结构节点
	 * @param node
	 * @return
	 */
	public static  Node formNode(org.htmlparser.Node node){
		
		if(node==null){
			return null;
		}
		if(!(node instanceof Tag)){
			return null;
		}
		Tag tag=(Tag)node;
		
		TagNode elementNode=new TagNode(tag.getTagName(), tag.getTagName(), null,LEVEL_1);
		//节点描述
		elementNode.setText(tag.getText());
		//
		elementNode.setSiber(SIBER_1);
		elementNode.setAttributeList(TagAttributeUtil.toHtmlAttribute(tag.getAttributesEx()));
		NodeList nodeList=tag.getChildren();
		if(nodeList!=null){
			for(int i=0;i<nodeList.size();i++){
				//遍历递归
				rescuitNode(nodeList.elementAt(i), elementNode);
			}
		}
		return elementNode;
	}
	
	private static void rescuitNode(org.htmlparser.Node node,Node elementNode){
		
		Node childElement=null;
		if(node==null){
			return;
		}
		if(node instanceof Tag){
			Tag tag=(Tag)node;
			//子节点
			childElement=new TagNode(tag.getTagName(), tag.getTagName(),elementNode.getNodeNo(),((TagNode) elementNode).getLevel()+1);
			//节点描述
			childElement.setText(tag.getText());
			childElement.setParentNode(elementNode);
			childElement.setParentNodeNo(elementNode.getNodeNo());
			//转换
			TagNode tagNode=(TagNode)childElement;
			tagNode.setAttributeList(TagAttributeUtil.toHtmlAttribute(tag.getAttributesEx()));
			//设置该子节点的顺序
			childElement.setSiber(addSiber(elementNode.getChildNodes().size()));
			//父节点新增子节点
			elementNode.addChildNode(childElement);
			
			NodeList nodeList=tag.getChildren();
			if(nodeList==null){
				return;
			}else{
				org.htmlparser.Node temp=null;
				for(int i=0;i<nodeList.size();i++){
					temp=nodeList.elementAt(i);
					rescuitNode(temp, childElement);
					
				}
			}
		}else{
			childElement=new TextNode(elementNode.getNodeNo(), ((TagNode) elementNode).getLevel()+1);
			childElement.setNodeName(node.getText());
			childElement.setNodeNo("html_text");
			//节点描述
			childElement.setText(node.getText());
			//设置该子节点的顺序
			childElement.setSiber(addSiber(elementNode.getChildNodes().size()));
			elementNode.addChildNode(childElement);
		}
	}
	
	/**
	 * 获得顺序
	 * @param siber
	 * @return
	 */
	private static Integer addSiber(Integer siber){
		return siber+1;
	}
	
	
	
	
}
